package heap;
import columnar.*;

import java.io.*;



import global.*;
import bufmgr.*;
import diskmgr.*;

public class TupleScan implements GlobalConst{

    public ColumnarFile  _cf;
    public TID _tids;
    public Scan[] _sc;
    public Heapfile[] hf1;
    public AttrType[] attrType;
//    public short REC_LEN1 = 32;
    public int[] targetcolnos;
   public TupleScan(){}
    public TupleScan(ColumnarFile cf) 
    throws InvalidTupleSizeException,
       IOException, HFException, HFBufMgrException, HFDiskMgrException
  {
        this._cf = cf;
        //this._sc = new Scan[_cf.numColumns];
        this.attrType = _cf.attrtype;
        String Fname;
        
        hf1 = new Heapfile[_cf.numColumns];
        _sc = new Scan[_cf.numColumns];
        
       
    for( int i=0;i<_cf.numColumns;i++)
    {      Fname = _cf.columnfileName+i;
    	try
    	{
        this.hf1[i] = new Heapfile(Fname);
    	}
    	catch(Exception e)
    	{
    		System.out.println("In tuplescan"+e);
    	}
        this. _sc[i]= new Scan(hf1[i]);
    }
      //  System.out.println("reached tuplescan");
  }
   
    //sri
    //target column names implementation  - new constructor added with targetcolno passed
      public TupleScan(ColumnarFile cf,int[] targetcolno) 
      throws InvalidTupleSizeException,
         IOException, HFException, HFBufMgrException, HFDiskMgrException
    {
      	
          this._cf = cf;
          //this._sc = new Scan[_cf.numColumns];
          this.attrType = _cf.attrtype;
          String Fname;
          
          hf1 = new Heapfile[targetcolno.length];
          _sc = new Scan[targetcolno.length];
          

          targetcolnos =targetcolno;
      for( int i=0;i<targetcolno.length;i++)
      {      Fname = _cf.columnfileName+(targetcolno[i]-1);
      	try
      	{
          this.hf1[i] = new Heapfile(Fname);
      	}
      	catch(Exception e)
      	{
      		System.out.println("In tuplescan"+e);
      	}
          this. _sc[i]= new Scan(hf1[i]);
      }
          System.out.println("reached tuplescan");
    }
      //srivi
      
      //sri

//    public Tuple getNext(TID tid) throws InvalidTypeException, InvalidTupleSizeException, IOException, FieldNumberOutOfBoundException, HFException, HFBufMgrException, HFDiskMgrException
//    {
//        String s;
//        int p;
//        String Fname;
//
//        Tuple tup = new Tuple();
//		 int k =0;
//			for(AttrType at : this._cf.attrtype)
//				{
//				if(at.attrType==0)
//					k++;
//				}
//				short[] tsize = new short[k];
//				for(int l=0;l<k;l++)
//				{
//		           tsize[l] = strlen;
//				}
//		short num =(short) this._cf.numColumns;
//		 tup.setHdr(num, this._cf.attrtype, tsize); // num = 4
//		Tuple t = new Tuple(tup.size());
//		 t.setHdr(num,this._cf.attrtype, tsize);
//		 
//       
//        for(int i=0;i<tid.numRIDs;i++)
//        {
//        	Tuple t1 = new Tuple();
//			AttrType[] at = new AttrType[1];
//			at[0] = new AttrType(this._cf.attrtype[i].attrType);
//	
//			short[] tsize1 = new short[1];
//		
//			if(this._cf.attrtype[i].attrType==0)
//			{
//	           tsize1[0] = strlen;
//	          
//			}
//			
//			 t1.setHdr((short) 1, at, tsize1);
//			
//		    Tuple tup1 = new Tuple(t1.size());
//		    tup1.setHdr((short) 1, at, tsize1);
//		    RID rid2 = new RID();
//            if(this._cf.attrtype[i].attrType == 0 )
//        {
//            	
//           tup1 = this._sc[i].getNext(rid2);
//           if (tup1 == null)
//           {
//        	   return null;
//           }
//           t1.tupleCopy(tup1);
//           try
//           {
//        	   t.setStrFld(i+1, t1.getStrFld(1));
//           }
//           catch(Exception e)
//           {
//        	   System.out.println(e);
//           }
//        }
//            else
//        {
//            	tup1 = this._sc[i].getNext(rid2);
//                t1.tupleCopy(tup1);
//                t.setIntFld(i+1, t1.getIntFld(1));
//           
//        }
//       
//            }
//        return t;
//       
//    }
      public Tuple getNext(TID tid) throws InvalidTypeException, InvalidTupleSizeException, IOException, FieldNumberOutOfBoundException, HFException, HFBufMgrException, HFDiskMgrException
      {
      //	System.out.println("getnext ");
          String s;
          int p;
          String Fname;
  //int[] attrtype;
          Tuple tup = new Tuple();
  		 int k =0;
  		 AttrType[] attrtarget= new AttrType[targetcolnos.length]; 
  		 for(int i=0;i<targetcolnos.length;i++)
  		 {
  			 attrtarget[i]=this.attrType[targetcolnos[i]-1];
  			if( this.attrType[targetcolnos[i]-1].attrType == 0)
  				k++;
  		 }
//  			
  				short[] tsize = new short[k];
  				for(int l=0;l<k;l++)
  				{
  		           tsize[l] = strlen;
  				}
  		short num =(short) this.targetcolnos.length;
  		 tup.setHdr(num, attrtarget, tsize); // num = 4
  		Tuple t = new Tuple(tup.size());
  		 t.setHdr(num,attrtarget, tsize);
  		 
         
 // 		 System.out.println("Target col length"+ targetcolnos.length);
          for(int i=0;i<targetcolnos.length;i++)
          {
          	Tuple t1 = new Tuple();
  			AttrType[] at = new AttrType[1];
  			at[0] = new AttrType(attrtarget[i].attrType);
  	
  			short[] tsize1 = new short[1];
  		
  			if(attrtarget[i].attrType==0)
  			{
  	           tsize1[0] = strlen;
  	          
  			}
  			
  			 t1.setHdr((short) 1, at, tsize1);
  			
  		    Tuple tup1 = new Tuple(t1.size());
  		    tup1.setHdr((short) 1, at, tsize1);
  		    RID rid2 = new RID();
              if(attrtarget[i].attrType == 0 )
          {
              	
             tup1 = this._sc[i].getNext(rid2);
             if (tup1 == null)
               {
            	   return null;
               }
             t1.tupleCopy(tup1);
             try
             {
          	   t.setStrFld(i+1, t1.getStrFld(1));
             }
             catch(Exception e)
             {
          	   System.out.println(e);
             }
          }
              else
          {
              	tup1 = this._sc[i].getNext(rid2);
                  t1.tupleCopy(tup1);
                  t.setIntFld(i+1, t1.getIntFld(1));
             
          }
        //      t.print(at);   
       }
       //  t.print(attrtarget);
        return t;
         
      }  


   
    public  boolean position(TID tid)
    throws InvalidTupleSizeException,
       IOException, HFException, HFBufMgrException, HFDiskMgrException
       {
        String Fname;

         boolean br=true;
        for(int i=0;i<_tids.numRIDs;i++)
        {
             br = _sc[_cf.numColumns].position(_tids.recordIDs[i]);
             if( br == false)
                 break;
             else
                 continue;       
       
       }
       return br;
       }
        /** Closes the Scan object
         * @throws IOException
         * @throws HFDiskMgrException
         * @throws HFBufMgrException
         * @throws HFException */
    public void closescan() throws HFException, HFBufMgrException, HFDiskMgrException, IOException
    {
        for( int i=0;i<_cf.numColumns;i++)
        {
               _sc[_cf.numColumns].closescan();
        }
    }

}